<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>订单管理</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="手机号" v-model="pagination.phone" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" clearable></el-input>
                <el-date-picker v-model="pagination.date" type="date" placeholder="         选择日期" style="width: 200px;" class="filter-item" @change="handleDateChange"></el-date-picker>
                <el-button @click="handleFilter()" class="dalfBut">查询</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="orderDate" label="预约日期" align="center"></el-table-column>
                <el-table-column prop="member.name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="member.phoneNumber" label="手机号" align="center"></el-table-column>
                <el-table-column prop="setmeal.name" label="套餐名称" align="center"></el-table-column>
                <el-table-column prop="orderStatus" label="体检状态" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button
                                :type="scope.row.orderStatus === '未到诊' ? 'primary' : 'info'"
                                :disabled="scope.row.orderStatus === '已到诊'"
                                @click="handleSign(scope.row)"
                        >
                            签到
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: { //分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 0,
                queryString: '', //
            },
            dataList: [], //列表数据
            member: {
                name: '',
                phoneNumber: ''
            },
            setmeal: {
                name: ''
            },
            orderDate: ''
        },
        created() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                //封装分页查询条件
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString,
                };
                // 发送ajax请求，进行分页查询
                axios.get("/orderList/findPage.do", { params: param }).then((res) => {
                    this.dataList = res.data.rows.map(row => {
                        row.orderDate = this.formatDate(new Date(row.orderDate));
                        return row;
                    });
                    this.pagination.total = res.data.total;
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            // 查询
            handleFilter() {
                this.pagination.currentPage = 1; // 重置当前页为第一页
                // 使用 this.pagination.phone 进行手机号查询
                if (this.pagination.phone) {
                    this.pagination.queryString = this.pagination.phone;
                }
                // 格式化日期
                if (this.pagination.queryString instanceof Date) {
                    this.pagination.queryString = this.formatDate(this.pagination.queryString);
                }
                this.findPage();
            },
            // 日期格式化方法
            formatDate(date) {
                const year = date.getFullYear();
                const month = String(date.getMonth() + 1).padStart(2, '0');
                const day = String(date.getDate()).padStart(2, '0');
                return `${year}-${month}-${day}`;
            },
            // 日期选择器改变事件
            handleDateChange() {
                if (this.pagination.date) {
                    this.pagination.queryString = this.formatDate(this.pagination.date);
                } else {
                    this.pagination.queryString = '';
                }
            },
            // 签到操作
            handleSign(row) {
                if (row.id === null) {
                    this.$message.error("订单ID不能为空");
                    return;
                }
                axios.get("/orderList.do", { params: { id: row.id } }).then((res) => {
                    if (res.data.flag) {
                        this.$message.success(res.data.message);
                        this.findPage();
                    } else {
                        this.$message.error(res.data.message);
                    }
                }).catch((error) => {
                    console.error("签到失败", error);
                    this.$message.error("签到失败");
                });
            }
        }
    })
</script>